package org.exercise.record.service;

import org.exercise.record.domain.User;
import org.exercise.record.repository.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDao userDao;

    @Override
    public List<User> listUser() {
        return userDao.listUser();
    }

    @Override
    public User getByUsername(String username) {
        return userDao.getByUsername(username);
    }

    @Override
    public List<User> findUsersByEmailContaining(String email) {
        return userDao.findByEmailContaining(email);
    }

    @Override
    public User loginService(String username, String password) {
        User user = userDao.getByUnameAndPassword(username, password);
        if (user != null) {
            // 确保重要信息不回返回前端
            user.setPassword("");
        }
        return user;
    }

    @Override
    public int registerService(User rUser) {
        if (userDao.getByUsername(rUser.getUsername()) != null) {
            // 无法注册
            return -1;
        } else {
            // 返回成功创建的用户对象
            int insertResult = userDao.insertUser(rUser);
            System.out.println(insertResult);
            return insertResult;
        }
    }

}
